import java.util.*;

/**
 * XML字符串生成工具类测试
 * 
 * @author AI Assistant
 * @version 1.0
 */
public class XmlStringGeneratorUtilTest {
    
    public static void main(String[] args) {
        System.out.println("=== XML字符串生成工具类测试 ===\n");
        
        // 测试1：字符串按字母排序
        testStringSort();
        
        // 测试2：简单XML元素生成
        testSimpleElement();
        
        // 测试3：带属性的XML元素生成
        testElementWithAttributes();
        
        // 测试4：完整XML文档生成
        testXmlDocument();
        
        // 测试5：XML特殊字符转义
        testXmlEscape();
        
        // 测试6：字符串列表排序连接
        testStringListSort();
        
        System.out.println("\n=== 测试完成 ===");
    }
    
    /**
     * 测试字符串按字母排序功能
     */
    private static void testStringSort() {
        System.out.println("1. 测试字符串按字母排序:");
        
        String[] testStrings = {
            "hello",
            "world",
            "dcba",
            "zyxwvu",
            "Hello World!",
            "测试中文字符",
            "123abc",
            ""
        };
        
        for (String str : testStrings) {
            String sorted = XmlStringGeneratorUtil.sortStringAlphabetically(str);
            System.out.println("  原字符串: \"" + str + "\" -> 排序后: \"" + sorted + "\"");
        }
        System.out.println();
    }
    
    /**
     * 测试简单XML元素生成
     */
    private static void testSimpleElement() {
        System.out.println("2. 测试简单XML元素生成:");
        
        String[][] testCases = {
            {"name", "john"},
            {"title", "hello world"},
            {"content", "zyxwvu"},
            {"data", ""},
            {"info", null}
        };
        
        for (String[] testCase : testCases) {
            try {
                String xml = XmlStringGeneratorUtil.generateSimpleElement(testCase[0], testCase[1]);
                System.out.println("  " + xml);
            } catch (Exception e) {
                System.out.println("  错误: " + e.getMessage());
            }
        }
        System.out.println();
    }
    
    /**
     * 测试带属性的XML元素生成
     */
    private static void testElementWithAttributes() {
        System.out.println("3. 测试带属性的XML元素生成:");
        
        // 测试用例1：单个属性
        Map<String, String> attrs1 = new HashMap<>();
        attrs1.put("id", "1");
        String xml1 = XmlStringGeneratorUtil.generateElementWithAttributes("user", attrs1, "admin");
        System.out.println("  " + xml1);
        
        // 测试用例2：多个属性（会按属性名排序）
        Map<String, String> attrs2 = new HashMap<>();
        attrs2.put("type", "text");
        attrs2.put("id", "input1");
        attrs2.put("class", "form-control");
        attrs2.put("name", "username");
        String xml2 = XmlStringGeneratorUtil.generateElementWithAttributes("input", attrs2, "default value");
        System.out.println("  " + xml2);
        
        // 测试用例3：无属性
        String xml3 = XmlStringGeneratorUtil.generateElementWithAttributes("span", null, "text content");
        System.out.println("  " + xml3);
        
        System.out.println();
    }
    
    /**
     * 测试完整XML文档生成
     */
    private static void testXmlDocument() {
        System.out.println("4. 测试完整XML文档生成:");
        
        // 创建测试元素
        List<XmlStringGeneratorUtil.XmlElement> elements = new ArrayList<>();
        
        // 添加title元素
        XmlStringGeneratorUtil.XmlElement titleElement = 
            new XmlStringGeneratorUtil.XmlElement("title", "hello world");
        elements.add(titleElement);
        
        // 添加author元素（带属性）
        Map<String, String> authorAttrs = new HashMap<>();
        authorAttrs.put("email", "test@example.com");
        authorAttrs.put("role", "writer");
        XmlStringGeneratorUtil.XmlElement authorElement = 
            new XmlStringGeneratorUtil.XmlElement("author", "john doe", authorAttrs);
        elements.add(authorElement);
        
        // 添加date元素
        XmlStringGeneratorUtil.XmlElement dateElement = 
            new XmlStringGeneratorUtil.XmlElement("date", "2024-01-01");
        elements.add(dateElement);
        
        // 添加content元素
        XmlStringGeneratorUtil.XmlElement contentElement = 
            new XmlStringGeneratorUtil.XmlElement("content", "this is content");
        elements.add(contentElement);
        
        // 生成XML文档
        String xmlDoc = XmlStringGeneratorUtil.generateXmlDocument("document", elements, "UTF-8");
        System.out.println(xmlDoc);
        System.out.println();
    }
    
    /**
     * 测试XML特殊字符转义
     */
    private static void testXmlEscape() {
        System.out.println("5. 测试XML特殊字符转义:");
        
        String[] testStrings = {
            "Hello & World",
            "<tag>content</tag>",
            "\"quoted text\"",
            "'single quotes'",
            "Mixed: <>&\"'",
            "Normal text without special chars"
        };
        
        for (String str : testStrings) {
            String escaped = XmlStringGeneratorUtil.escapeXmlSpecialChars(str);
            System.out.println("  原字符串: \"" + str + "\"");
            System.out.println("  转义后: \"" + escaped + "\"");
            System.out.println();
        }
    }
    
    /**
     * 测试字符串列表排序连接
     */
    private static void testStringListSort() {
        System.out.println("6. 测试字符串列表排序连接:");
        
        List<String> stringList1 = Arrays.asList("zebra", "apple", "banana", "cherry");
        String result1 = XmlStringGeneratorUtil.sortAndJoinStrings(stringList1, ", ");
        System.out.println("  列表1: " + stringList1);
        System.out.println("  排序连接结果: " + result1);
        System.out.println();
        
        List<String> stringList2 = Arrays.asList("hello", "world", "java", "xml");
        String result2 = XmlStringGeneratorUtil.sortAndJoinStrings(stringList2, " | ");
        System.out.println("  列表2: " + stringList2);
        System.out.println("  排序连接结果: " + result2);
        System.out.println();
        
        // 测试空列表
        List<String> emptyList = new ArrayList<>();
        String result3 = XmlStringGeneratorUtil.sortAndJoinStrings(emptyList, ",");
        System.out.println("  空列表结果: \"" + result3 + "\"");
        System.out.println();
    }
} 